import ast
import json
import random

from django.views import View
from django.http import JsonResponse
import jwt
from app.models import *

from app.respositories.respositories import *
from app.services.services import *
from app.tests import *
from django.http import HttpResponseServerError


# 获取轮播图
class GetBannerView(View):
    def get(self, request):
        list = get_banner()
        return JsonResponse({
            "code": 200,
            "list": list
        })


# 发布动态
class PublishBlogView(View):
    def post(self, request):
        data = json.loads(request.body)
        publish_blog(data)
        return JsonResponse({
            "code": 200,
            "msg": "发布成功"
        })


# 获取动态
class GetBlogView(View):
    def get(self, request):
        try:
            lists = get_blog()
            return JsonResponse({
                "code": 200,
                "data": {
                    "result": lists,
                    "fileUrl": get_server_url(request)
                }
            })
        except Exception as e:
            return JsonResponse({
                "code": 500,
                "msg": "查询不到" + str(e),
            })


# 送礼物
class SendGiftView(View):
    def post(self, request):
        try:
            params = json.loads(request.body)
            print(type(json.loads(request.body)), "body参数")
            id = params['id']
            blog = params['blog']
            gift = params['gifts']
            send_user = params['send_user']
            gift_name = params['gift_name']
            post_params = {
                "id": id,
                "blog": blog,
                "gifts": gift,
                "send_user": send_user,
                'gift_name': gift_name
            }
            status = send_gift(post_params)
            if status:
                return JsonResponse({
                    "code": 200,
                    "msg": "赠送成功"
                })
            else:
                return JsonResponse({
                    "code": 200,
                    "msg": "赠送失败"
                })
        except Exception as e:
            print(e)
            print(f"出错啦！{e}")
            return JsonResponse({
                "code": 400,
                "msg": str(e)
            })


class GetGiftView(View):
    def get(self, request):
        data = get_all_gifts()
        return JsonResponse({
            "code": 200,
            "data": {
                "result": data
            }
        })


# 添加礼物
class PostGiftView(View):
    def post(self, request):
        name = request.POST.get('name', "")
        price = request.POST.get('price', "")
        image = request.POST.get('image', "")
        post_params = {
            "name": name,
            "price": price,
            "image": image
        }
        status = commit_gift(post_params)
        if status:
            return JsonResponse({
                "code": 200,
                'msg': "添加礼物成功"
            })
        else:
            return JsonResponse({
                "code": 500,
                'str': "出错了"
            })


# 获取所有礼物
class GetGiftsView(View):
    def get(self, request):
        data = get_all_gifts()
        return JsonResponse({
            "code": 200,
            "data": {
                "result": data
            }
        })


# 添加用户信息
class PostAttentionView(View):
    def post(self, request):
        # 循环十次
        for i in range(1, 11):
            generate_random_userprofile()
        return JsonResponse({
            "code": 200,
            "msg": "模拟数据成功"
        })


class TestView(View):
    def get(self, request):
        if random.choice([True, False]):
            return HttpResponseServerError({
                'code': 500,
                'msg': "500.html"
            })
        return JsonResponse({
            'code': 200
        })
